package cn.itcast.usermanager.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.usermanager.pojo.User;
import cn.itcast.usermanager.service.UserService;

/**
 * 使用rest风格进行编程
 * 
 * @author Administrator
 *
 */
@RequestMapping("rest/user")
@Controller
public class RestUserController {

	@Autowired
	private UserService userService;

	/**
	 * 根据id查询用户
	 * 
	 * @param id
	 * @return
	 * @date 2016年4月16日下午1:51:32
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<User> queryUserById(@PathVariable("id") Long id) {
		try {
			User user = userService.queryUserById(id);
			if (user == null) {
				// 资源没有找到,响应404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
				
				
			}
			// 资源存在,响应200
			// return ResponseEntity.status(HttpStatus.OK).body(user);
			return ResponseEntity.ok(user);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(null);
		}
	}

	/**
	 * 新增用户
	 * 
	 * @param user
	 * @return
	 * @date 2016年4月16日下午1:51:23
	 */
	@RequestMapping(method = RequestMethod.POST)
	public ResponseEntity<Void> insertUser(User user) {
		try {
			Integer num = userService.addUser(user);
			if (num > 0) {
				// 新增成功
				return ResponseEntity.status(HttpStatus.CREATED).body(null);
			}
			// 新增失败
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(null);
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.body(null);
		}
	}

	/**
	 * 修改用户
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT)
	public ResponseEntity<Void> updateUser(User user) {
		try {
			// 调用业务层方法更新
			Integer num = userService.updateUser(user);
			if (num > 0) {
				// 更新成功
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
			// 更新失败
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.build();
		} catch (Exception e) {
			e.printStackTrace();
			// 代码运行出现异常
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.build();
		}
	}

	/**
	 * 删除用户
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE)
	public ResponseEntity<Void> deleteUserByIds(
			@RequestParam("ids") String[] ids) {
		try {
			Integer num = userService.deleteUserByIds(ids);
			if (num > 0) {
				// 删除成功
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			}
			// 删除失败
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.build();
		} catch (Exception e) {
			e.printStackTrace();
			// 代码出现异常
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
					.build();
		}

	}
}
